package com.gofbox.config;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import com.gofbox.util.PropertiesUtils;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * springboot集成mybatis的基本入口
 * 1）创建数据源
 * 2）创建SqlSessionFactory
 */
@MapperScan("com.box.**.mapper")
@Configuration    //该注解类似于spring配置文件
public class MyBatisConfig {

    public MyBatisConfig(){
        System.out.println("sql init  ...");
    }
    /**
     * 创建数据源
     */
    @Bean
    public DataSource dataSource1() throws Exception{
        Properties props = new Properties();
        props.put("driverClassName", PropertiesUtils.findPorpertyByKey("spring.datasource.driver-class-name","mysql_db1.properties"));
        props.put("url", PropertiesUtils.findPorpertyByKey("spring.datasource.url","mysql_db1.properties"));
        props.put("username", PropertiesUtils.findPorpertyByKey("spring.datasource.username","mysql_db1.properties"));
        props.put("password", PropertiesUtils.findPorpertyByKey("spring.datasource.password","mysql_db1.properties"));
        return DruidDataSourceFactory.createDataSource(props);
    }

    /**
     * 根据数据源创建SqlSessionFactory
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource ds) throws Exception{
        SqlSessionFactoryBean fb = new SqlSessionFactoryBean();
        fb.setDataSource(dataSource1());//指定数据源(这个必须有，否则报错)
        //下边两句仅仅用于*.xml文件，如果整个持久层操作不需要使用到xml文件的话（只用注解就可以搞定），则不加
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        fb.setMapperLocations(resolver.getResources(PropertiesUtils.findPorpertyByKey("mybatis.mapperLocations","mysql_db1.properties")));
        fb.setTypeAliasesPackage(PropertiesUtils.findPorpertyByKey("mybatis.typeAliasesPackage","mysql_db1.properties"));//指定基包
//        fb.setTypeHandlersPackage("com.box.user.model");
//        fb.setMapperLocations(resolver.getResources("classpath:/mybatis/sqlmap/user/*.xml"));



       // fb.setMapperLocations(new PathMatchingResourcePatternResolver().getResources(env.getProperty("mybatis.mapperLocations")));//指定xml文件位置

        return fb.getObject();
    }


  /*  @Bean
    public DataSourceTransactionManager transactionManager() throws Exception {
        return new DataSourceTransactionManager(dataSource1());
    }*/

    @Bean
    public PlatformTransactionManager transactionManager() {
        try{
            return new DataSourceTransactionManager(dataSource1());

        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;
    }

}